﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Context;

namespace Agile.Engine
{
    /// <summary>
    /// Spring.Net 工具集
    /// </summary>
    public static class ObjectContext
    {
        #region Static

        static ObjectContext()
        {
            ResourceFactory = new Xml.XmlResourceFactory() { ConfigUri = Environment.DefaultResourceFactory };
        }


        /// <summary>
        /// 获取注入对象上下文
        /// </summary>
        public static IApplicationContext DefaultContext
        {
            get
            {
                IApplicationContext ctx = null;
                try
                {
                    ctx = Spring.Context.Support.ContextRegistry.GetContext("agileContext");
                }
                catch
                {
                    ctx = new Spring.Context.Support.XmlApplicationContext(Environment.DefaultAppContext);
                    ctx.Name = "agileContext";
                    Spring.Context.Support.ContextRegistry.RegisterContext(ctx);
                }
                return ctx;
            }
            set { Spring.Context.Support.ContextRegistry.RegisterContext(value); }
        }

        /// <summary>
        /// 获取注入对象
        /// </summary>
        /// <param name="alias"></param>
        /// <returns></returns>
        public static object GetObject(string alias)
        {
            return DefaultContext[alias];
        }

        /// <summary>
        /// 数据源提供对象
        /// </summary>
        /// <param name="provider"></param>
        /// <param name="connstr"></param>
        /// <returns></returns>
        public static Spring.Data.Common.IDbProvider CustomProvider(string provider, string connstr)
        {
            var db = Spring.Data.Common.DbProviderFactory.GetDbProvider(provider);
            db.ConnectionString = connstr;
            return db;
        }

        /// <summary>
        /// 数据连接工厂
        /// </summary>
        public static Xml.DataProviderResource DataProviderFactory
        {
            get
            {
                if (ResourceFactory.ContainsKey("DataProviderFactory"))
                {
                    return ResourceFactory["DataProviderFactory"] as Xml.DataProviderResource;
                }

                var dpr = new Xml.DataProviderResource() { ConfigUri = Environment.DefaultResourceFactory };
                ResourceFactory["DataProviderFactory"] = dpr;
                return dpr;
            }
        }

        /// <summary>
        /// 数据连接
        /// </summary>
        /// <param name="alias"></param>
        /// <returns></returns>
        public static Spring.Data.Common.IDbProvider GetDbProvider(string alias)
        {
            return DataProviderFactory.CreateDataProvider(alias);
        }

        /// <summary>
        /// 创建数据访问模板
        /// </summary>
        /// <param name="alias"></param>
        /// <returns></returns>
        public static Spring.Data.Core.AdoTemplate CreateTemplate(string alias)
        {
            return DataProviderFactory.CreateTemplate(alias);
        }

        /// <summary>
        /// 资源工厂
        /// </summary>
        public static Agile.Engine.Xml.XmlResourceFactory ResourceFactory
        { get; set; }

        /// <summary>
        /// 获取资源对象
        /// </summary>
        /// <param name="alias"></param>
        /// <returns></returns>
        public static Agile.Engine.Xml.XmlResource GetResource(string alias)
        {
            return ResourceFactory[alias] as Agile.Engine.Xml.XmlResource;
        }

        #endregion

        #region Environment

        /// <summary>
        /// 系统环境信息
        /// </summary>
        public static class Environment
        {
            /// <summary>
            /// 环境注册表
            /// </summary>
            static Dictionary<string, string> registry { get; set; }

            /// <summary>
            /// 获取环境注册所有信息
            /// </summary>
            public static System.Collections.Hashtable Registry
            { get { return new System.Collections.Hashtable(registry); } }

            /// <summary>
            /// 根目录
            /// </summary>
            public static string Bin { get { return registry[InnerResource.EI_Bin].ToString(); } }
            /// <summary>
            /// 配置文件目录
            /// </summary>
            public static string Config { get { return registry[InnerResource.EI_Config].ToString(); } }
            /// <summary>
            /// 数据文件目录
            /// </summary>
            public static string Data { get { return registry[InnerResource.EI_Data].ToString(); } }
            /// <summary>
            /// 插件目录
            /// </summary>
            public static string Plugin { get { return registry[InnerResource.EI_Plugin].ToString(); } }
            /// <summary>
            /// 资源目录
            /// </summary>
            public static string Resource { get { return registry[InnerResource.EI_Resource].ToString(); } }
            /// <summary>
            /// 更新目录
            /// </summary>
            public static string Update { get { return registry[InnerResource.EI_Update].ToString(); } }
            /// <summary>
            /// 组件目录
            /// </summary>
            public static string Components { get { return registry[InnerResource.EI_Update].ToString(); } }

            static Environment()
            {

                registry = new Dictionary<string, string>();
                registry[InnerResource.EI_Bin] = AppDomain.CurrentDomain.BaseDirectory;
                registry[InnerResource.EI_Config] = System.IO.Path.Combine(Bin, InnerResource.EI_Config);
                registry[InnerResource.EI_Data] = System.IO.Path.Combine(Bin, InnerResource.EI_Data);
                registry[InnerResource.EI_Plugin] = System.IO.Path.Combine(Bin, InnerResource.EI_Plugin);
                registry[InnerResource.EI_Resource] = System.IO.Path.Combine(Bin, InnerResource.EI_Resource);
                registry[InnerResource.EI_Update] = System.IO.Path.Combine(Bin, InnerResource.EI_Update);
                registry[InnerResource.EI_Components] = System.IO.Path.Combine(Bin, InnerResource.EI_Components);
            }

            /// <summary>
            /// 注册
            /// </summary>
            /// <param name="key"></param>
            /// <param name="value"></param>
            public static void Registe(string key, string value)
            {
                registry[key] = value;
            }

            /// <summary>
            /// 获取
            /// </summary>
            /// <param name="key"></param>
            /// <returns></returns>
            public static string Resolve(string key)
            {
                if (registry.ContainsKey(key))
                    return registry[key];
                return string.Empty;
            }

            internal static string DefaultAppContext
            {
                get
                {
                    ///运行目录下的config/appContext.config
                    var defaultConfig = System.IO.Path.Combine(Config, InnerResource.AppContext);
                    if (System.IO.File.Exists(defaultConfig))
                        return defaultConfig;
                    ///网站目录下的config/appContext.config
                    if (System.Web.HttpContext.Current != null)
                    {
                        defaultConfig = System.Web.HttpContext.Current.Server.MapPath(@"config\appContext.config");
                        if (System.IO.File.Exists(defaultConfig))
                            return defaultConfig;
                    }
                    ///Agile.Engine.Config下的appContext.config
                    return InnerResource.AssemblyConfig + InnerResource.AppContext;
                }
            }

            internal static string DefaultResourceFactory
            {
                get
                {
                    ///运行目录下的config/appContext.config
                    var defaultConfig = System.IO.Path.Combine(Config, InnerResource.AppResource);
                    if (System.IO.File.Exists(defaultConfig))
                        return defaultConfig;
                    ///网站目录下的config/appContext.config
                    if (System.Web.HttpContext.Current != null)
                    {
                        defaultConfig = System.Web.HttpContext.Current.Server.MapPath(@"config\appResource.config");
                        if (System.IO.File.Exists(defaultConfig))
                            return defaultConfig;
                    }
                    ///Agile.Engine.Config下的appContext.config
                    return InnerResource.AssemblyConfig + InnerResource.AppResource;
                }
            }

            internal static string DefaultDataFactory
            {
                get
                {
                    ///运行目录下的config/appContext.config
                    var defaultConfig = System.IO.Path.Combine(Config, InnerResource.AppData);
                    if (System.IO.File.Exists(defaultConfig))
                        return defaultConfig;
                    ///网站目录下的config/appContext.config
                    if (System.Web.HttpContext.Current != null)
                    {
                        defaultConfig = System.Web.HttpContext.Current.Server.MapPath(@"config\appData.config");
                        if (System.IO.File.Exists(defaultConfig))
                            return defaultConfig;
                    }
                    ///Agile.Engine.Config下的appContext.config
                    return InnerResource.AssemblyConfig + InnerResource.AppData;
                }
            }
        }
        #endregion

    }
}
